package com.example.administrator.timertask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.timertask.util.TimeUtil;
import com.example.administrator.timertask.weight.MarqueeView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Handler mHandler = new Handler();
    private boolean isPause = false;//是否暂停
    private long currentSecond ;//当前毫秒数
    MarqueeView tvMarquee;
    private TextView tvProfitOne;
    private TextView tvProfitTwo;
    private TextView tvProfitThree;
    private TextView tvProfitFour;
    private TextView tvProfitFive;
    private TextView scroll_tv;
    private HorizontalScrollView mNoticeScrollView;
    private TranslateAnimation mRigthToLeftAnim;
    private final static float SCOLL_V = 0.2f;
    private LinearLayout ll_marquee;
    private boolean isStart = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initAdapter();
    }

    private void initAdapter() {
    }

    private void initView() {
        ll_marquee = findViewById(R.id.ll_marquee);
        textView = findViewById(R.id.time);

        mNoticeScrollView = (HorizontalScrollView) findViewById(R.id.horiSv);

        ll_marquee.post(new Runnable() {
            @Override
            public void run() {
                mRigthToLeftAnim = new TranslateAnimation(mNoticeScrollView.getWidth(), -ll_marquee.getWidth(), 0, 0);
                mRigthToLeftAnim.setRepeatCount(Animation.INFINITE);
                mRigthToLeftAnim.setInterpolator(new LinearInterpolator());
                mRigthToLeftAnim.setDuration((long) ((mNoticeScrollView.getWidth() + ll_marquee.getWidth()) / SCOLL_V));
                ll_marquee.startAnimation(mRigthToLeftAnim);
                isStart = true;
            }
        });

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(MainActivity.this, HorizontalScrollViewActivity.class));
               // startActivity(new Intent(MainActivity.this,TimerSimpleActivity.class));
                startActivity(new Intent(MainActivity.this, TimerActivity.class));
             /*   if(ll_marquee != null && isStart) {
                    ll_marquee.clearAnimation();
                    isStart = false;
                }else {
                    isStart = true;
                    ll_marquee.startAnimation(mRigthToLeftAnim);
                }*/
            }
        });

        //initMarqueeView();
        startTime(currentSecond);
        //startActivity(new Intent(this,LikeActivity.class));

    }

    private void initMarqueeView() {
    /*    View view = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.scroll_textview, null);
        View view2 = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.scroll_textview2, null);
        View view3 = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.scroll_textview3, null);
        View view4 = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.scroll_textview4, null);
        View view5 = (LayoutInflater.from(MainActivity.this)).inflate(R.layout.scroll_textview5, null);
        tvMarquee.addViewInQueue(view);
        tvMarquee.addViewInQueue(view2);
        tvMarquee.addViewInQueue(view3);
        tvMarquee.addViewInQueue(view4);
        tvMarquee.addViewInQueue(view5);
        tvProfitOne = view.findViewById(R.id.tv_profit_one);
        tvProfitTwo = view2.findViewById(R.id.tv_profit_two);
        tvProfitThree = view3.findViewById(R.id.tv_profit_three);
        tvProfitFour = view4.findViewById(R.id.tv_profit_four);
        tvProfitFive = view5.findViewById(R.id.tv_profit_five);

        tvProfitOne.setText("恭喜张航11.00元");
        tvProfitTwo.setText("恭喜战美22.33元");
        tvProfitThree.setText("恭喜赵猫33.12元");
        tvProfitFour.setText("恭喜王544.23元");
        tvProfitFive.setText("恭喜李四99.00元");

        tvMarquee.requestFocus();//获取焦点
        tvMarquee.setScrollSpeed(10);//滚动速度
        tvMarquee.setScrollDirection(MarqueeView.RIGHT_TO_LEFT);//滚动方向
        tvMarquee.setViewMargin(8);//按钮间隔mk
        tvMarquee.startScroll();*/
    }

    private void startTime(long currentSecond) {

            mHandler.postDelayed(timeRunnable, 1000);
    }

    Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            if(currentSecond>0) {
                currentSecond = currentSecond - 1000;
                Log.e("currentSecond",currentSecond+"");
                textView.setText(TimeUtil.getFormatHMS(currentSecond));
                if (!isPause) {
                    mHandler.postDelayed(timeRunnable, 1000);
                }
            }else {
                Message message = new Message();
                message.what = 1;
                handlerStop.sendMessage(message);
            }
        }
    };

    final Handler handlerStop = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    currentSecond = 0;
                    mHandler.removeCallbacks(timeRunnable);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(timeRunnable);
        timeRunnable = null;
        currentSecond = 0;
    }
}
